using System;
using System.Collections.Generic;

using Bug.Net.Common.DataObjects;
using Bug.Net.Business.BusinessObjects;

using NHibernate;
using NHibernate.Criterion;
using CureConsulting.DataAccess;

namespace Bug.Net.DataAccess.DataControls
{
	public partial class LoginDataControl : IDataControl<LoginBO>
	{
		#region FieldNames
		
		/// <summary>
        /// Struct to hold field names
        /// </summary>
		public struct FieldNames
        {
			/// <summary>
        	/// HQL Property Name for Id
        	/// </summary>
			public const string Id = "Id";
			/// <summary>
        	/// HQL Property Name for Active
        	/// </summary>
			public const string Active = "Active";
			/// <summary>
        	/// HQL Property Name for Email
        	/// </summary>
			public const string Email = "Email";
			/// <summary>
        	/// HQL Property Name for FirstName
        	/// </summary>
			public const string FirstName = "FirstName";
			/// <summary>
        	/// HQL Property Name for LastName
        	/// </summary>
			public const string LastName = "LastName";
			/// <summary>
        	/// HQL Property Name for UserName
        	/// </summary>
			public const string UserName = "UserName";
			/// <summary>
        	/// HQL Property Name for Password
        	/// </summary>
			public const string Password = "Password";
			/// <summary>
        	/// HQL Property Name for Salt
        	/// </summary>
			public const string Salt = "Salt";
			/// <summary>
        	/// HQL Property Name for PasswordQuestion
        	/// </summary>
			public const string PasswordQuestion = "PasswordQuestion";
			/// <summary>
        	/// HQL Property Name for PasswordAnswer
        	/// </summary>
			public const string PasswordAnswer = "PasswordAnswer";
			/// <summary>
        	/// HQL Property Name for CreateDate
        	/// </summary>
			public const string CreateDate = "CreateDate";
			/// <summary>
        	/// HQL Property Name for Roles
        	/// </summary>
			public const string Roles = "Roles";
		}
		
		#endregion
		
		#region Private Members
		
		private static LoginDataControl loginDataControl;
		private static object lockLoginDataControl = new object();
		private static NHibernateDataControl<Login> dataControl = new NHibernateDataControl<Login>();
		
		#endregion
		
		#region Constructors
		
		/// <summary>
		/// Constructor for login.
		/// </summary>
		public LoginDataControl(){}
		
		#endregion
		
		#region Private Methods

        private static IList<LoginBO> ConvertPOCOListToBOList(IList<Login> logins)
        {
            List<LoginBO> retVal = new List<LoginBO>();

            if (logins != null)
            {
                foreach (Login login in logins)
                {
                    retVal.Add(new LoginBO(login));
                }
            }

            return retVal;
        }

        #endregion
		
		#region Public Properties

        /// <summary>
        ///Singleton Instance
        /// </summary>
        public static LoginDataControl Instance
        {
            get
            {
                lock (lockLoginDataControl)
                {
                    if(loginDataControl == null)
                    {
                        loginDataControl = new LoginDataControl();
                    }
                }

                return loginDataControl;
            }
        }

        #endregion
		
		#region CRUD

        /// <summary>
        /// Function to Save a Login
        /// </summary>
        /// <param name="login"></param>
        /// <returns></returns>
        public int? Save(LoginBO login)
        {
            int? retVal = dataControl.Save(login.POCO);
            if (retVal.HasValue)
            	dataControl.Refresh(login.POCO);
            return retVal;
        }

        /// <summary>
        /// Function to Save a list of logins
        /// </summary>
        /// <param name="logins"></param>
        /// <returns></returns>
        public bool Save(IList<LoginBO> logins)
        {
            List<Login> itemsToSave = new List<Login>();

            foreach (LoginBO login in logins)
            {
                itemsToSave.Add(login.POCO);
            }

            return dataControl.Save(itemsToSave);
        }

        /// <summary>
        /// Function to delete a Login
        /// </summary>
        /// <param name="login"></param>
        /// <returns></returns>
        public bool Delete(LoginBO login)
        {
            return dataControl.Delete(login.POCO);
        }

        /// <summary>
        /// Function to Delete a list of logins
        /// </summary>
        /// <param name="logins"></param>
        /// <returns></returns>
        public bool Delete(IList<LoginBO> logins)
        {
            List<Login> itemsToDelete = new List<Login>();

            foreach (LoginBO login in logins)
            {
                itemsToDelete.Add(login.POCO);
            }

            return dataControl.Delete(itemsToDelete);
        }		
		
        #endregion
		
		#region Queries
		
		/// <summary>
        /// Get Login by ID
        /// </summary>
        /// <param name="id">ID of Login</param>
        /// <returns>The Login</returns>
        public LoginBO GetById(int? id)
        {
			Login retVal = dataControl.GetObjectById(id);
			
			if(retVal != null)
				return new LoginBO(retVal);
			
			return null;
		}
		
		/// <summary>
        /// Return list of all logins
        /// </summary>
        /// <returns></returns>
        public IList<LoginBO> GetAll()
        {
			IList<Login> retVal = dataControl.GetQueryResult(dataControl.GetCriteria());

            if (retVal != null)
                return ConvertPOCOListToBOList(retVal);
         
			return new List<LoginBO>();
        }
		
		/// <summary>
        /// Return list of all logins
        /// </summary>
        /// <param name="firstResult"></param>
        /// <param name="maxResults"></param>
        /// <returns></returns>
        public IList<LoginBO> GetAll(int firstResult, int maxResults)
        {
			IList<Login> retVal = dataControl.GetQueryResult(dataControl.GetCriteria(), firstResult, maxResults);

            if (retVal != null)
                return ConvertPOCOListToBOList(retVal);
            
			return new List<LoginBO>();
        }
		
		/// <summary>
        /// Return list of all logins
        /// </summary>
        /// <param name="firstResult"></param>
        /// <param name="maxResults"></param>
		/// <param name="sortExpression"></param>
        /// <returns></returns>
        public IList<LoginBO> GetAll(int firstResult, int maxResults, string sortExpression)
        {
			ICriteria criteria = dataControl.GetCriteria();

            if (!String.IsNullOrEmpty(sortExpression))
            {
                string[] sort = sortExpression.Split(' ');
                bool ascending = true;
                if (sort.Length > 1 && sort[1].ToUpper() == "DESC") { ascending = false; }
                criteria.AddOrder(new NHibernate.Criterion.Order(sort[0],ascending));
            }
			
			IList<Login> retVal = dataControl.GetQueryResult(criteria, firstResult, maxResults);

            if (retVal != null)
                return ConvertPOCOListToBOList(retVal);
        
			return new List<LoginBO>();
        }
		
		/// <summary>
        /// Return count of all logins
        /// </summary>
        /// <returns></returns>
        public int GetCountOfAll()
        {			
			int result = (int)dataControl.GetCriteria().SetProjection(Projections.RowCount()).UniqueResult();
			return result;
        }
		
		/// <summary>
        /// Query to get logins by Email
        /// </summary>
		/// <param name="email">Email of the Login</param>
		/// <param name="firstResult"></param>
        /// <returns>The logins</returns>
        public LoginBO GetByEmail(string email)
        {
			ICriteria criteria = dataControl.GetCriteria();
            criteria.Add(Expression.Eq(FieldNames.Email, email));
						
			Login retVal = dataControl.GetUniqueQueryResult(criteria);
			
			if(retVal != null)
				return new LoginBO(retVal);
				
			return null;	
        }
		
		/// <summary>
        /// Query to get logins by UserName
        /// </summary>
		/// <param name="userName">UserName of the Login</param>
		/// <param name="firstResult"></param>
        /// <returns>The logins</returns>
        public LoginBO GetByUserName(string userName)
        {
			ICriteria criteria = dataControl.GetCriteria();
            criteria.Add(Expression.Eq(FieldNames.UserName, userName));
						
			Login retVal = dataControl.GetUniqueQueryResult(criteria);
			
			if(retVal != null)
				return new LoginBO(retVal);
				
			return null;	
        }
		
		#endregion
	}
}